#include<iostream>
#include<list>
#include<queue>
using namespace std;
class Map {
public:
	int n;
	list<int>* tmp;
	bool* visited;
	Map(int N) {
		n = N;
		tmp = new list<int>[n];
		visited = new bool[n];
		for (int i = 0; i < n; i++) {
			visited[i] = false;
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				char ch;
				cin >> ch;
				if (ch == '1') {
					tmp[i].push_back(j);
				}
			}
		}
	}
	void BFS() {
		visited[0] = true;
		queue<int> Q;
		Q.push(0);
		while (!Q.empty()) {
			int m = Q.front();
			cout << m << ' ';
			Q.pop();
			for (auto i = tmp[m].begin(); i != tmp[m].end(); i++) {
				if (!visited[*i]) {
					visited[*i] = true;
					Q.push(*i);
				}
			}
		}
		cout << endl;
	}
};
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n;
		cin >> n;
		Map M(n);
		M.BFS();
	}
	return 0;
}